package com.example.cruddemo;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    private final List<Product> productList = new ArrayList<>();
    private long nextId = 1;

    @GetMapping
    public List<Product> getAllProducts() {
        return productList;
    }

    @PostMapping
    public ResponseEntity<Product> createProduct(@RequestBody Product product)
    {
        product.setId(nextId++);
        productList.add(product);
        return new ResponseEntity<>(product, HttpStatus.CREATED);
    }
}
